﻿<UserControl x:Class="Passwords.Views.ChangePasswordView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:utils="clr-namespace:Passwords.Utils" xmlns:views="clr-namespace:Passwords.Views"
            xmlns:vm="clr-namespace:Passwords.ViewModels" xmlns:models="clr-namespace:Passwords.Models"
            xmlns:controls="clr-namespace:Passwords.Controls" xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:vr="clr-namespace:Passwords.ValidationRules"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="473" d:DesignWidth="845">
    <UserControl.InputBindings>
        <KeyBinding Key="Esc" Command="{x:Static vm:Commands.LogoutCommand}" />
    </UserControl.InputBindings>
    <UserControl.Resources>
        <ControlTemplate x:Key="template">
            <Grid Margin="0,5,10,0" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <TextBlock Text="{Binding rs.CPChangeMasterPassword, Source={StaticResource locale}}" FontSize="48" FontWeight="Light" VerticalAlignment="Top" Margin="10,-5,25,20" TextTrimming="CharacterEllipsis"/>
                <StackPanel VerticalAlignment="Top" HorizontalAlignment="Right" x:Name="appBtnPanel"
                        Margin="0,0,-4,0" Orientation="Horizontal">
                    <Button x:Name="changePwBtn" Content="{Binding rs.CPLoginBtn, Source={StaticResource locale}}" Style="{StaticResource radioLikeButtonStyle}"  Command="{x:Static vm:Commands.LogoutCommand}">
                        <Button.RenderTransform>
                            <TranslateTransform/>
                        </Button.RenderTransform>
                    </Button>
                </StackPanel>

                <Grid Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,-44,0,0">
                    <StackPanel x:Name="stack2" >
                        <StackPanel.InputBindings>
                            <KeyBinding Key="Enter" Command="{x:Static vm:Commands.ChangePasswordCommand}" CommandParameter="{Binding ElementName=passwordBinder}" />
                        </StackPanel.InputBindings>
                        <StackPanel.Resources>
                            <Style TargetType="PasswordBox">
                                <Setter Property="FontSize" Value="16"/>
                            </Style>
                        </StackPanel.Resources>
                        <TextBlock Text="{Binding rs.CPEnterCurrentMasterPassword, Source={StaticResource locale}}" 
                                   controls:FocusContainer.FocusDelay="0:0:0.20"
                                   FontSize="14" Margin="0,0,0,5"/>
                        <PasswordBox x:Name="oldPasswordBox" Width="240" />
                        <TextBlock  Text="{Binding rs.CPEnterNewMasterPassword, Source={StaticResource locale}}" FontSize="14" Margin="0,20,0,5"/>
                        <PasswordBox x:Name="newPasswordBox" Width="240"/>
                        <TextBlock  Text="{Binding rs.CPConfirmPassword, Source={StaticResource locale}}" FontSize="14" Margin="0,20,0,5"/>
                        <PasswordBox x:Name="confirmPasswordBox" Width="240" />
                        <controls:PasswordBinder x:Name="passwordBinder" Visibility="Collapsed"
                                     LoginBox="{Binding ElementName= oldPasswordBox}"
                                     NewPasswordBox="{Binding ElementName=newPasswordBox}"
                                     ConfirmPasswordBox="{Binding ElementName=confirmPasswordBox}"/>
                        <Button HorizontalAlignment="Right" Content="{Binding rs.CPChange, Source={StaticResource locale}}" Margin="0,20,0,5" Padding="12,2,12,2" Command="{x:Static vm:Commands.ChangePasswordCommand}" CommandParameter="{Binding ElementName=passwordBinder}" />
                    </StackPanel>
                    <StackPanel x:Name="errorPanel" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Collapsed">
                        <StackPanel.InputBindings>
                            <KeyBinding Key="Esc" Command="{x:Static vm:Commands.ShowChangePasswordCommand}"/>
                        </StackPanel.InputBindings>
                        <TextBlock Text="{Binding LoginErrorText}" Foreground="{DynamicResource AppAccentBrush}" />
                        <Button x:Name="okBtn" Content="{Binding rs.Ok, Source={StaticResource locale}}" Width="100" Margin="0,20,0,5" HorizontalAlignment="Center"
                                Command="{x:Static vm:Commands.ShowChangePasswordCommand}"/>
                    </StackPanel>
                    <StackPanel x:Name="busyPanel"  Visibility="Collapsed" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <TextBlock Text="{Binding rs.CPChangingPassword, Source={StaticResource locale}}" HorizontalAlignment="Center" Margin="4"/>
                        <ProgressBar x:Name="pbar" IsIndeterminate="True" IsEnabled="False" Width="200" Height="16"/>
                    </StackPanel>
                </Grid>
            </Grid>
            <ControlTemplate.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding AppDisplayMode}" Value="{x:Static models:AppDisplayMode.ChangePassword}" />
                        <Condition Binding="{Binding LoginError}" Value="False"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="controls:FocusContainer.IsFocused" TargetName="oldPasswordBox" Value="true" />
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding LoginError}" Value="True">
                    <Setter Property="Panel.Visibility" Value="Visible" TargetName="errorPanel"/>
                    <Setter Property="Panel.Visibility" Value="Hidden" TargetName="stack2"/>
                    <Setter Property="controls:FocusContainer.IsFocused" TargetName="okBtn" Value="true"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsBusy}" Value="True">
                    <Setter Property="Panel.Visibility" Value="Visible" TargetName="busyPanel"/>
                    <Setter Property="Panel.Visibility" Value="Hidden" TargetName="stack2"/>
                    <Setter Property="ProgressBar.IsEnabled" Value="true" TargetName="pbar"/>
                    <Setter Property="Control.IsEnabled" Value="False" TargetName="changePwBtn"/>
                </DataTrigger>

            </ControlTemplate.Triggers>
        </ControlTemplate>

    </UserControl.Resources>
    <UserControl.Style>
        <Style TargetType="UserControl">
            <!--<Setter Property="Template" Value="{StaticResource template}"/>-->
            <Style.Triggers>
                <DataTrigger Binding="{Binding AppDisplayMode}" Value="{x:Static models:AppDisplayMode.ChangePassword}" >
                    <Setter Property="Template" Value="{StaticResource template}"/>
                   
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>
</UserControl>
